package com.scs.application.modules.base.excel.handler;/**
 * @Description:
 */

import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.utils.Reflections;
import com.scs.application.core.utils.SpringHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.MaterialCatalog;
import com.scs.application.modules.base.service.MaterialCatalogService;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @ClassName MaterialCatalogDataHandler
 */
public class MaterialCatalogDataHandler extends ExcelDataHandlerDefaultImpl {
    private Map<String, MaterialCatalog> codeMap = Maps.newHashMap();

    public MaterialCatalogDataHandler() {
        List<MaterialCatalog> materialCatalogs = this.getMaterialCatalogService().list(Wrappers.<MaterialCatalog>query().select(new String[]{"id,code"}));
        for (MaterialCatalog materialCatalog : materialCatalogs) {
            this.codeMap.put(materialCatalog.getCode(), materialCatalog);
        }

    }

    public String[] getNeedHandlerFields() {
        return new String[]{"品类代码"};
    }

    public Object importHandler(Object obj, String name, Object value) {
        if (!"品类代码".equalsIgnoreCase(name)) {
            return null;
        }
        if (value == null || StringUtils.isBlank(value.toString())) {
            return null;
        }
        MaterialCatalog materialCatalog = this.codeMap.get(value.toString());
        if (materialCatalog != null && Reflections.getAccessibleField(obj, "catalogId") != null) {
            Reflections.setFieldValue(obj, "catalogId", materialCatalog.getId());
        }
        return null;
    }

    public MaterialCatalogService getMaterialCatalogService() {
        return SpringHolder.getBean(MaterialCatalogService.class);
    }
}
